﻿using ProductTool.Dao;
using ProductTool.Entity;
using ProductTool.Tools;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ProductTool.Service
{
    public class BrandService
    {
        public void Process() {
            BrandDao dao = new BrandDao();
            var list = dao.QueryAllBrand();            
            int pageIndex = 0;
            var current = list.Skip(pageIndex * 100).Take(100).ToList();
            while (current.Count>0)
            {
                System.Console.WriteLine($"创建第{pageIndex}批*100条");
                dao.CreateBrand(current);
                pageIndex++;
                current = list.Skip(pageIndex * 100).Take(100).ToList();
            }
            System.Console.WriteLine("品牌导入完成");
            System.Console.ReadKey();
        }

        public void ProcessCate()
        {
         
            BrandDao dao = new BrandDao();
            var list_source = dao.QueryCategory();
            var list = new List<CategoryEntity>();

            var c1_list = list_source.Distinct(new C1Comparer()).ToList();
            foreach (var item in c1_list)
            {
                CategoryEntity entity = new CategoryEntity();
                entity.category_id = item.c1_id;
                entity.category_name = item.c1_name;
                entity.category_name_eng = item.c1_name;
                entity.level = 1;
                entity.path = $"{item.c1_id}";
                entity.parent_category_id = 0;
                list.Add(entity);
            }
            var c2_list = list_source.Distinct(new C2Comparer()).ToList();
            foreach (var item in c2_list)
            {
                CategoryEntity entity = new CategoryEntity();
                entity.category_id = item.c2_id;
                entity.category_name = item.c2_name;
                entity.category_name_eng = item.c2_name;
                entity.level = 2;
                entity.path = $"{item.c1_id},{item.c2_id}";
                entity.parent_category_id = item.c1_id;
                list.Add(entity);
            }
            var c3_list = list_source.Distinct(new C3Comparer()).ToList();
            foreach (var item in c3_list)
            {
                CategoryEntity entity = new CategoryEntity();
                entity.category_id = item.c3_id;
                entity.category_name = item.c3_name;
                entity.category_name_eng = item.c3_name;
                entity.level = 3;
                entity.warning_days = 90;
                entity.path = $"{item.c1_id},{item.c2_id},{item.c3_id}";
                entity.categoryExtend = new CategoryExtend { minimal_exp = 90 };
                entity.parent_category_id = item.c2_id;             
                list.Add(entity);
            }
            list = list.OrderBy(p => p.level).ToList();
            foreach (var item in list)
            {
               // if (dao.CheckCategoryExists(item.category_id).category_name==null)
                {
                    dao.CreateCategory(new List<CategoryEntity> { item });
                    //var str = Newtonsoft.Json.JsonConvert.SerializeObject(item);
                    //var result = ServiceHelper.CallService<ResponseBase>("http://centralapi.ecnow.tech/im/category/insert", item, Common.token);
                    //if (result.messageId!=10000)
                    //{
                    //    System.Console.WriteLine($"==>{result.messageId},{result.zhError}");
                    //}
                    //else
                    //{
                    //    System.Console.WriteLine(item.category_id+"==>OK");
                    //}

                    System.Console.WriteLine(item.category_id + "==>Create OK");
                }               
            }

//            insert into yamibuy_im.im_category_sequence(category, sequence)
//select a.category_id,a.category_id sequence from yamibuy_im.im_category a
// left
//                                            join yamibuy_im.im_category_sequence b on a.category_id = b.category_id
//where b.category_id is null
            //var current = list.Skip(pageIndex * 100).Take(100).ToList();
            //while (current.Count > 0)
            //{
            //    System.Console.WriteLine($"创建第{pageIndex}批*100条");
            //    
            //    pageIndex++;
            //    current = list.Skip(pageIndex * 100).Take(100).ToList();
            //}
            System.Console.WriteLine("分类导入完成");
            System.Console.ReadKey();
        }
    }
    public class C1Comparer : IEqualityComparer<JDCategory>
    {
        public bool Equals(JDCategory x, JDCategory y)
        {
            return x.c1_id == y.c1_id;
        }

        public int GetHashCode(JDCategory obj)
        {
            return obj.ToString().GetHashCode();
        }
    }
    public class C2Comparer : IEqualityComparer<JDCategory>
    {
        public bool Equals(JDCategory x, JDCategory y)
        {
            return x.c2_id == y.c2_id;
        }

        public int GetHashCode(JDCategory obj)
        {
            return obj.ToString().GetHashCode();
        }
    }
    public class C3Comparer : IEqualityComparer<JDCategory>
    {
        public bool Equals(JDCategory x, JDCategory y)
        {
            return x.c3_id == y.c3_id;
        }

        public int GetHashCode(JDCategory obj)
        {
            return obj.ToString().GetHashCode();
        }
    }
}
